package com.markhsiu.MiniMQ.remote.transport.netty.handler;

import com.markhsiu.MiniMQ.broker.message.BrokerFactory;
import com.markhsiu.MiniMQ.model.Message;
import com.markhsiu.MiniMQ.model.ResponseMessage;
import com.markhsiu.MiniMQ.model.constant.MessageSourceEnum;
import com.markhsiu.MiniMQ.remote.ChannelHandler;
import com.markhsiu.MiniMQ.remote.MessageHandler;

public class ProducerNettyMessageHandler implements MessageHandler{

	private static final String BODY = "producer ask";
	 
	@Override
	public void handler(final Message request,ChannelHandler handler) {
 
		Message response = new ResponseMessage();
		response.setSource(MessageSourceEnum.BROKER);
		response.setTarget(MessageSourceEnum.PRODUCER);
		response.setTimestamp(System.nanoTime());
		if(request == null){
			response.setBody(FAIL.getBytes()); 
		} else {
			response.setBody(BODY.getBytes());
			response.setMessageID(request.getMessageID());
			BrokerFactory.instance().addMessage(request);
		}
	 
		handler.writeAndFlush(response);
	}
}
